package com.kingtsoft.study.controller;

import com.github.pagehelper.PageInfo;
import com.kingtsoft.study.common.ContextHolder;
import com.kingtsoft.study.common.JsonResult;
import com.kingtsoft.study.common.PageUtil;
import com.kingtsoft.study.constant.DescribableEnum;
import com.kingtsoft.study.constant.ParamConstants;
import com.kingtsoft.study.exception.BusinessException;
import com.kingtsoft.study.model.PageRequest;
import com.kingtsoft.study.model.StudyHomework;
import com.kingtsoft.study.model.StudyHomeworkDetail;
import com.kingtsoft.study.service.StudyHomeworkDetailService;
import com.kingtsoft.study.service.StudyHomeworkService;
import com.kingtsoft.study.util.FileUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 * Title:
 * Description:
 * Copyright: Copyright (c) 2017-03-30 下午2:12
 * Company: wondersgroup.com
 *
 * @author jerry.pan(潘健雷)
 * @version 1.0
 */
@Controller
@RequestMapping("study/homeworkDetail")
public class StudyHomeworkDetailController {

    @Autowired
    StudyHomeworkDetailService homeworkDetailService;

    @Autowired
    StudyHomeworkService homeworkService;

    /**
     * 添加作业页面
     *
     * @return
     * @author jerry.pan
     * @date 2017-03-30 14:54:07
     */
    @RequestMapping(value = "/page/add")
    public ModelAndView pageAdd(StudyHomeworkDetail homeworkDetail) {
        ModelAndView mav = new ModelAndView("/homework/homework_detail_edit");
        mav.addObject(ParamConstants.PAGE_TYPE_NAME, ParamConstants.PAGE_TYPE_ADD);
        String homeworkId = homeworkDetail.getHomeworkId();
        if (StringUtils.isNotBlank(homeworkId)) {
            StudyHomework homework = homeworkService.load(homeworkId);
            mav.addObject("homework", homework);
        }
        mav.addObject("homeworkDetail", homeworkDetail);
        return mav;
    }

    /**
     * 修改作业页面
     *
     * @return
     * @author jerry.pan
     * @date 2017-03-30 14:54:07
     */
    @RequestMapping(value = "/page/edit")
    public ModelAndView pageEdit(StudyHomeworkDetail homeworkDetail) {
        ModelAndView mav = new ModelAndView("/homework/homework_detail_edit");
        mav.addObject(ParamConstants.PAGE_TYPE_NAME, ParamConstants.PAGE_TYPE_EDIT);

        String id = homeworkDetail.getId();
        String homeworkId = homeworkDetail.getHomeworkId();
        StudyHomework homework = null;
        if (StringUtils.isNotBlank(id)) {
            homeworkDetail = homeworkDetailService.load(homeworkDetail.getId());
            homework = homeworkService.load(homeworkDetail.getHomeworkId());
        } else if (StringUtils.isNotBlank(homeworkId)) {
            if (StringUtils.isNotBlank(homeworkId)) {
                homework = homeworkService.load(homeworkId);
            }
            if (ContextHolder.getSessionStudent() == null) {
                throw new BusinessException(DescribableEnum.USER_IS_NOT_STUDENT);
            }
            String studentId = ContextHolder.getSessionStudent().getId();
            if (StringUtils.isBlank(studentId)) {
                throw new BusinessException(DescribableEnum.USER_IS_NOT_STUDENT);
            }
            StudyHomeworkDetail homeworkDetailNew = homeworkDetailService.loadByHomeworkIdAndStudentId(homeworkId, studentId);
            if (homeworkDetailNew != null) {
                homeworkDetail = homeworkDetailNew;
            }
        }
        String path = homeworkDetail.getPath();
        if (StringUtils.isNotBlank(path)) {
            try {
                String url = FileUtil.getFileUrl(path);
                homeworkDetail.setUrl(url);
            } catch (IOException e) {
            }
        }
        mav.addObject("homeworkDetail", homeworkDetail);
        mav.addObject("homework", homework);
        return mav;
    }

    /**
     * 添加作业页面
     *
     * @return
     * @author jerry.pan
     * @date 2017-03-30 14:54:07
     */
    @RequestMapping(value = "/page/view")
    public ModelAndView pageView(StudyHomeworkDetail homeworkDetail) {
        ModelAndView mav = new ModelAndView("/homework/homework_detail_edit");
        mav.addObject(ParamConstants.PAGE_TYPE_NAME, ParamConstants.PAGE_TYPE_VIEW);
        String id = homeworkDetail.getId();
        if (StringUtils.isNotBlank(id)) {
            homeworkDetail = homeworkDetailService.load(homeworkDetail.getId());
            if (homeworkDetail != null) {
                try {
                    String path = homeworkDetail.getPath();
                    if (StringUtils.isNotBlank(path)) {
                        String url = FileUtil.getFileUrl(path);
                        homeworkDetail.setUrl(url);
                    }
                } catch (IOException e) {
                }
                String homeworkId = homeworkDetail.getHomeworkId();
                if (StringUtils.isNotBlank(homeworkId)) {
                    StudyHomework homework = homeworkService.load(homeworkId);
                    mav.addObject("homework", homework);
                }
            }
        }
        mav.addObject("homeworkDetail", homeworkDetail);
        return mav;
    }

    /**
     * 增加/修改作业
     *
     * @param homeworkDetail
     * @return
     * @author jerry.pan
     * @date 2017-03-30 14:54:00
     */
    @RequestMapping("/edit")
    @ResponseBody
    public JsonResult edit(StudyHomeworkDetail homeworkDetail) {
        homeworkDetailService.edit(homeworkDetail);
        JsonResult jsonResult = new JsonResult(DescribableEnum.SUCCESS, homeworkDetail);
        return jsonResult;
    }


    /**
     * 上传文件
     *
     * @param file
     * @return JsonResult
     * url 下载地址
     * @author jerry.pan(潘健雷)
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult upload(MultipartFile file, StudyHomeworkDetail homeworkDetail) {
        homeworkDetail = homeworkDetailService.saveUpload(file, homeworkDetail);
        JsonResult jsonResult = new JsonResult(DescribableEnum.SUCCESS, homeworkDetail);
        return jsonResult;
    }

    /**
     * 删除文件
     *
     * @return JsonResult
     * url 下载地址
     * @author jerry.pan(潘健雷)
     */
    @RequestMapping(value = "/deleteFile", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult deleteFile(@RequestParam String id) {
        homeworkDetailService.deleteFile(id);
        JsonResult jsonResult = new JsonResult(DescribableEnum.SUCCESS);
        return jsonResult;
    }


    /**
     * 已提交作业的学生作业列表
     *
     * @return
     * @author jerry.pan
     * @date 2017-03-30 14:54:07
     */
    @RequestMapping(value = "/page/listCommitted")
    public ModelAndView pageListCommitted(StudyHomeworkDetail homeworkDetail) {
        ModelAndView mav = new ModelAndView("/homework/homework_committed_list");
        mav.addObject("user", ContextHolder.getSessionUser());
        mav.addObject("student", ContextHolder.getSessionStudent());
        mav.addObject("teacher", ContextHolder.getSessionTeacher());
        mav.addObject("homeworkDetail", homeworkDetail);
        return mav;
    }

    /**
     * 已提交作业的学生作业列表
     *
     * @return
     * @author jerry.pan
     * @date 2017-03-30 14:54:07
     */
    @RequestMapping("/listCommittedPage")
    @ResponseBody
    public JsonResult listCommittedPage(PageRequest pageRequest, StudyHomeworkDetail homeworkDetail) {
        PageInfo page = PageUtil.getPage(pageRequest);
        PageInfo<StudyHomeworkDetail> result = homeworkDetailService.listCommittedPage(page, homeworkDetail);
        JsonResult jsonResult = new JsonResult(DescribableEnum.SUCCESS, result);
        return jsonResult;
    }
}
